/*************************************************************************
	> File Name: 2280.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-04-23 13:02:57
 ************************************************************************/

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX_N 5000

int g[MAX_N + 5][MAX_N + 5];

int main() {
    int n, m;
    cin >> n >> m;
    int max_x = m, max_y = m;
    for (int i = 0, x, y, w; i < n; i++) {
        cin >> x >> y >> w;
        g[x + 1][y + 1] += w; //（0, 0）-> (1, 1)，炸弹位置可以重复
        max_x = max(x + 1, max_x);
        max_y = max(y + 1, max_y);
    }
    for (int i = 1; i <= max_x; i++) {
        for (int j = 1; j <= max_y; j++) {
            g[i][j] += g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1];
        }
    }
    int ans = 0;
    for (int i = 1; i + m - 1 <= max_x; i++) {
        for (int j = 1; j + m - 1 <= max_y; j++) {
            int k = m + i - 1, l = m + j - 1;
            int val = g[k][l] - g[i - 1][l] - g[k][j - 1] + g[i - 1][j - 1];
            ans = max(ans, val);
        }
    }
    cout << ans << endl;
    return 0;
}
